from rest_framework import serializers
from apps.headline.models import Channel, Article, Comment
from apps.questions.models import Label
from apps.users.models import User


# 频道列表
class ChannelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Channel
        fields = '__all__'

class ArticlesSerializer(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = '__all__'

# 用户文章
class UserArticleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = ['id', 'title']


# 用户
class UserSerializer(serializers.ModelSerializer):
    articles = UserArticleSerializer(many=True)
    class Meta:
        model = User
        fields = ['id', 'username', 'avatar', 'articles', 'fans']


# 文章展示
class ArticleSerializer(serializers.ModelSerializer):
    collected = serializers.BooleanField(default=False)
    user = UserSerializer(read_only=True)

    class Meta:
        model = Article
        fields = ['id', 'title', 'content', 'createtime', 'user', 'collected_users', 'collected', 'image']


# 收藏
class CollectedSerializer(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = '__all__'


# subs
class SubCommentSerializer(serializers.ModelSerializer):
    user = UserSerializer()

    class Meta:
        model = Comment
        fields = ['id', 'content', 'article', 'user', 'parent', 'subs', 'createtime']


# 评论
class CommentsSerializer(serializers.ModelSerializer):
    user = UserSerializer()
    subs = SubCommentSerializer(many=True)

    class Meta:
        model = Comment
        fields = ['id', 'user', 'subs', 'content', 'createtime', 'updatetime', 'parent', 'article']

# 标签
class LabelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Label
        fields = '__all__'


# 文章信息
class ArticleInfoSerializer(serializers.ModelSerializer):
    user = UserSerializer()
    comments = CommentsSerializer(many=True)
    labels = LabelSerializer(many=True)

    class Meta:
        model = Article
        fields = ['id', 'user', 'comments', 'title', 'content', 'image', 'createtime', 'updatetime', 'visits',
                  'thumbup', 'comment_count', 'channel', 'labels', 'collected_users']

class ArticleCreateSerializer(serializers.ModelSerializer):
    user_id = serializers.IntegerField()
    lables = serializers.IntegerField()
    class Meta:
        model = Article
        fields = ['id','content','lables','title','channel','image','user_id']


class ArticleSearchSerializer(serializers.ModelSerializer):
    text = serializers.StringRelatedField(label='')

    class Meta:
        model = Article
        fields = ['id','text','createtime','content','title']
